package com.elves.cloud.controller;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.datetime.standard.DateTimeFormatterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.time.LocalDateTime;

@Getter
@Slf4j
@Controller
// @RequestMapping("/server")
public class AppAction {

    static LocalDateTime time = LocalDateTime.now();

    public static void setTime(LocalDateTime timeNew) {
        time = timeNew;
    }

    // 不需要认证，不需要角色
    @GetMapping(value = "/time")
    @ResponseBody
    public ResponseEntity<Object> home() {
        return new ResponseEntity<>(
                "Welcome to elves auth! 當前时间："
                        + LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter()),
                HttpStatus.OK);
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello(Principal principal) {
        log.info("principal:{}", principal);
//        log.info("principal:{}.getName()", principal.getName());
//        return "hello";
        return principal.toString();
    }

    @GetMapping(value = "/gitee/{id}")
    @ResponseBody
    public ResponseEntity<Object> giteeID(@PathVariable String id) {
        return new ResponseEntity<>(
                "elves gitee Welcome 【" + id + "】 at "
                        + LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter()),
                HttpStatus.OK);

    }

    @GetMapping(value = "/projects/{id}")
    @ResponseBody
    public ResponseEntity<Object> projectsID(@PathVariable String id) {
        return new ResponseEntity<>(
                "elves projects Welcome 【" + id + "】 at "
                        + LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter()),
                HttpStatus.OK);

    }

    @GetMapping(value = "/openid/{id}")
    @ResponseBody
    public ResponseEntity<Object> openidID(@PathVariable String id) {
        return new ResponseEntity<>(
                "elves openid Welcome 【" + id + "】 at "
                        + LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter()),
                HttpStatus.OK);

    }


    // 需要认证，需要角色
    @GetMapping(value = "/admin/{id}")
    @ResponseBody
    public ResponseEntity<Object> adminID(@PathVariable String id) {
        return new ResponseEntity<>(
                "elves admin Welcome 【" + id + "】 at "
                        + LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter()),
                HttpStatus.OK);

    }

    @GetMapping(value = "/user/{id}")
    @ResponseBody
    public ResponseEntity<Object> userID(@PathVariable String id) {
        return new ResponseEntity<>(
                "elves user Welcome 【" + id + "】 at "
                        + LocalDateTime.now().format(new DateTimeFormatterFactory().createDateTimeFormatter()),
                HttpStatus.OK);
    }


}
